// 使用 Mock
var express = require('express');
var Mock = require('mockjs')

let app=express();
//设置跨域访问
app.all('*', (req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With,Authorization,Content-Type");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

app.use("/test",function(req,res){
	res.json(Mock.mock({
    name: {
        first: '@FIRST',
        middle: '@FIRST',
        last: '@LAST',
        full: '@first @middle @last'
    }
}));	
});

app.use("/demo",function(req,res){
	res.json(Mock.mock({
    name: Mock.Random.boolean()
}));	
});

app.use("/user/login",function(req,res){
    if(req.query.mobileNumber=='13160237122'&&req.query.pwd=='123456'){
        res.json(Mock.mock({
            code:200,
            msg:"sucess",
            data: 'p2urio2hr90y234iop2yu0s9phro2h2ojroiyd09qwhjwoeiyrfwo9'
        }));
    }else{
        res.json(Mock.mock({
            code:401,
            msg:"手机号或密码错误",
            data: ''
        }));
    }
});

app.use("/orderss",function(req,res){
    var data=Mock.mock({
        "orderss|100-200":[
            {
                "orderId":'@id',
                "orderSn": '@csentence(3, 5)',
                "orderName":'@cname',
                "num|1-10":0,
                "price|70-600.2":0
            }
        ]
    });
    var page={
        orderss:data.orderss.slice(0,6)
    }
    res.send({
        code:200,
        msg:'success',
        data:page
    })		
});

app.use("/users",function(req,res){
    var data=Mock.mock({
        "users|100-300":[
            {
                "userId":'@id',
                "userName":'@cname',
                "sex|0-1":0,
                "mobileNumber":/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
            }
        ]
    });
    var page={
        users:data.users.slice(0,10),
        total:data.users.length
    }
    res.send({
        code:200,
        msg:'success',
        data:page
    })		
});

app.use("/categories",function(req,res){
    var data=Mock.mock({
        "categories|5-10":[
            {
                "categoryId":'@id',
                "categorySn": /[a-z][A-Z]{4,5}/,
                "categoryName":'@cword(3)',
                "parentCategorySn":'',
                "img":'@image("60x60","#FF6600","@categoryName")'
            }
        ]
    });
    var dataArray = data.categories;
    //生成子类
    data.categories.forEach(element => {
        var categories = Mock.mock({
            "categories|3-6":[
                {
                    "categoryId":'@id',
                    "categorySn": /[a-z][A-Z]{4,5}/,
                    "categoryName":'@cword(3)',
                    "parentCategorySn":element.categorySn,
                    "img":'@image("60x60","#FF6600","@categoryName")'
                }
            ]
        }).categories;
        categories.forEach(element => {
            element.categorySn =element.parentCategorySn+"-"+element.categorySn
        });
        dataArray = dataArray.concat(categories);
    });
   

    res.send({
        code:200,
        msg:'success',
        data:dataArray
    })		
});

app.use("/products",function(req,res){

    var categories=Mock.mock({
        "categories|6-12":[
            {
                "categoryId":'@id',
                "categorySn": /[a-z][A-Z]{4,5}/,
                "categoryName":'@cword(3)',
                "parentCategorySn":''
            }
        ]
    }).categories;
    //生成子类
    categories.forEach(element => {
        var list = Mock.mock({
            "categories|5-7":[
                {
                    "categoryId":'@id',
                    "categorySn": /[a-z][A-Z]{4,5}/,
                    "categoryName":'@cword(3)',
                    "parentCategorySn":element.categorySn
                }
            ]
        }).categories;
        list.forEach(element => {
            element.categorySn =element.parentCategorySn+"-"+element.categorySn
        });
        categories = categories.concat(list);
    });


    var data=Mock.mock({
        "products|50-100":[
            {
                "productId":'@id',
                "productName":'@csentence(3, 5)',
                "category":function(){
                    var num = Math.floor(Math.random() * (categories.length-1));
                    return categories[num];
                },
                "categoryName":function() {
                    let category = this.category
                    return category.categoryName
                  },
                "categorySn":function() {
                    let category = this.category
                    return category.categorySn
                  },
                "price|1-200.2":0,
                "stock|1-200":0,
                "imgs":function(){
                    var imgs = [];
                    var img1 = {
                        name:"img1.png",
                        url:Mock.mock('@image("200x200","#FF6600","商品")')
                    }
                    var img2 = {
                        name:"img2.png",
                        url:Mock.mock('@image("200x200","#FF6600","33")')
                    }
                    var img3 = {
                        name:"img3.png",
                        url:Mock.mock('@image("200x200","#FF6600","123")')
                    }
                    imgs.push(img1);
                    imgs.push(img2);
                    imgs.push(img3);
                    return imgs;
                }
            }
        ]
    });
    console.log(data.products);
    var dataArray = data.products;
    var page={
        products:dataArray.slice(0,10),
        categories:categories,
        total:dataArray.length
    }

    res.send({
        code:200,
        msg:'success',
        data:page
    })		
});

app.listen(9999,()=>{console.log('listen 9999')});